<?php

namespace App\Dao;

use App\Model\SettingsModel;
use App\Services\Cache;
use App\Services\ChatServices;
use Illuminate\Support\Facades\DB;
use Psr\SimpleCache\InvalidArgumentException;

class SettingsDao extends BaseDao
{
    public static function get_settings_builder_query($params = []){
        $query = SettingsModel::query();

        $query->when(checkIsHasField($params,'id'),function ($query)use ($params){
            $query->where('id',$params['id']);
        });

        $query->when(checkIsHasField($params,'key_name'),function ($query)use ($params){
            $query->where('key_name',$params['key_name']);
        });

        return $query;
    }



    public static function getOne($params = []){
        return self::get_settings_builder_query($params)->first();
    }



    public static function buildParms(): array
    {
        $list = self::get_settings_builder_query()->get()->toArray();
        $lists_datas = [];
        foreach ($list as $key=>$value){
            $lists_datas[$value['key_name']] = $value['key_text'];
        }
        return $lists_datas;
    }

    public static function update_settings(array $params = [])
    {
        unset($params['s']);
        DB::beginTransaction();
        $res = true;
        foreach ($params as $key=>$value){
            $res !== false && $res = self::get_settings_builder_query([
                'key_name'=>$key
            ])->update([
                'key_text'=>$value
            ]);
        }
        if($res === false){
            DB::rollBack();
            self::error('保存失败');
        }
        DB::commit();
    }

    public static function get_seven($user_id)
    {
        $start_time = strtotime(date('Y-m-d'));
        $end_time = strtotime(date('Y-m-d'))+86400;
        $data = [];
        for($i=0;$i<7;$i++){
            $data[$i] = OrdersDao::get_order_child_builder([
                'h_user_id'=>$user_id,
                'status'=>2
            ])->where('hexiao_time','>=',$start_time)
            ->where('hexiao_time','<',$end_time)->count();
            $start_time = $start_time-86400;
            $end_time = $end_time-86400;
        }
        return $data;
    }

    public static function get_count_all()
    {
        return [
            'yunying'=>UsersDao::get_shop_builder()->count(),
            'count'=>UsersDao::get_user_builder()->count()
        ];
    }

    public static function update_fanxian(array $params)
    {
        $timeRange = explode(' - ',$params['timeRange']);
        $params['users_buy_start_time'] = strtotime($timeRange[0]);
        $params['users_buy_end_time'] = strtotime($timeRange[1]);
        unset($params['timeRange']);
        unset($params['s']);
        DB::beginTransaction();
        $res = true;
        foreach ($params as $key=>$value){
            $res !== false && $res = self::get_settings_builder_query([
                'key_name'=>$key
            ])->update([
                'key_text'=>$value
            ]);
        }
        if($res === false){
            DB::rollBack();
            self::error('保存失败');
        }
        DB::commit();
    }

}
